import { GetSquardPosion } from "./work/state"
import { isInArray } from "@/utils"
import { getStandCreep } from "./work/state"
import { squardAttackDirection, squardHealDirection } from "./work/state"
import { leftConst, rightConst, squardDirection, tactical } from "./work/constant"
import { squardCross, squardLeft, squardRight } from "./work/action"

/* 判断爬虫是否含有攻击部件 */
export function AttackBody(creep:Creep):boolean{
    for (var i of creep.body)
    {
        if (i.type == 'attack' || i.type == 'ranged_attack')
        return true
    }
    return false
}

/* 爬虫攻击部件数据 */
export function AttackBodyData(creep:Creep):{[bo:string]:number}{
    var result = {'attack':0,'ranged_attack':0}
    for (var i of creep.body)
    {
        if (i.type == 'attack')
        {
            if (!i.boost) result['attack'] += 30
            else if (i.boost == 'UH') result['attack'] += 60
            else if (i.boost == 'UH2O') result['attack'] += 90
            else if (i.boost == 'XUH2O') result['attack'] += 120
        }
        else if (i.type == 'ranged_attack')
        {
            if (!i.boost) result['ranged_attack'] += 10
            else if (i.boost == 'KO') result['ranged_attack'] += 20
            else if (i.boost == 'KHO2') result['ranged_attack'] += 30
            else if (i.boost == 'XKHO2') result['ranged_attack'] += 40
        }
    }
    return result
}

/* 某一部件的数据 */
export function BodyNum(creep:Creep,bodyName:BodyPartConstant):number{
    var s = 0
    for (var i of creep.body)
    {
        if (i.type == bodyName) s++
    }
    return s
}

/* 判断四人小队是否还具备活性 已转移 */
export function SquardActive(id:string):boolean{
    if (!Memory.squardMemory) Memory.squardMemory = {}
    if (!Memory.squardMemory[id]) return false
    for (var cName in Memory.squardMemory[id].creepData)
    {
        if (!Game.creeps[cName]) return false
    }
    return true
}

/* 获取四人小队中指定类型的爬 已转移 */
export function SquardCreep(id:string,str:string):Creep{
    if (!Memory.squardMemory) Memory.squardMemory = {}
    for (var cName in Memory.squardMemory[id].creepData)
    {
        if (Memory.squardMemory[id].creepData[cName].position == str && Game.creeps[cName])
            return Game.creeps[cName]
    }
    return null
}

/* 判断是否已经集合到位 已转移 */
export function identifyReady(squardData:Squard):boolean
{

    var standCreep:Creep
    var thisRoom:string
    for (var cName in squardData)
    {
        if (squardData[cName].position == '↖' && Game.creeps[cName])
        {
            thisRoom = Game.creeps[cName].room.name
            standCreep = Game.creeps[cName]
        }
    }
    if (!standCreep) return false
    for (var cName in squardData)
    {
        var disPos = GetSquardPosion(standCreep,squardData[cName].position)
        if (!Game.creeps[cName] || ( Game.creeps[cName].room.name == thisRoom&&disPos &&!Game.creeps[cName].pos.isEqualTo(disPos)))
            return false
    }
    return true
}

/* 判断所有爬虫是否已经到了目标房间 已转移 */
export function identifyArrivedRoom(squardData:Squard,disRoom:string):boolean{
    for (var cName in squardData)
    {
        if (!Game.creeps[cName]) continue
        if (Game.creeps[cName].room.name != disRoom) return false
    }
    return true
}
/**
 *    战术动作方向常量
 *    8 1 2
 *    7   3
 *    6 5 4
 */





/* 返回目标和自己的方向,如果null说明方向一致 */
export function idenfityPosDirection(squardData:Squard,pos:RoomPosition):string{
    var Attackdirection = squardAttackDirection(squardData)
    if (Attackdirection == null) return null
        /* 计算位置和小队的距离 */
        var standCreep = getStandCreep(squardData)
        if (!standCreep) return
        var Xdistance = standCreep.pos.x - pos.x
        var direction_:string = null
        var Ydistance = standCreep.pos.y - pos.y
        var absXdistance = Math.abs(Xdistance)
        var absYdistance = Math.abs(Ydistance)
        if (Xdistance > 0)
        {
            if (Ydistance > 0)
            {
                if(absXdistance < absYdistance)
                {
                    direction_ = '↑'
                }
                else if (absXdistance == absYdistance)
                {
                    direction_ = '←'
                }
                else if (absXdistance > absYdistance)
                {
                    direction_ = '←'
                }
            }
            else if (Ydistance <= 0 && Ydistance > -2)
            {
                if(absXdistance < absYdistance)
                {
                    direction_ = '←'
                }
                else if (absXdistance == absYdistance)
                {
                    direction_ = '←'
                }
                else if (absXdistance > absYdistance)
                {
                    direction_ = '←'
                }
            }
            else if (Ydistance <= -2 )
            {
                if(absXdistance < (absYdistance- 1))
                {
                    direction_ = '↓'
                }
                else if (absXdistance == (absYdistance- 1))
                {
                    direction_ = '←'
                }
                else if (absXdistance > (absYdistance- 1))
                {
                    direction_ = '←'
                }
            }
    
        }
        else if (Xdistance <= 0 && Xdistance >-2)
        {
            if (Ydistance > 0)
            {
                if(absXdistance < absYdistance)
                {
                    direction_ = '↑'
                }
                else if (absXdistance == absYdistance)
                {
                    direction_ = '↑'
                }
                else if (absXdistance > absYdistance)
                {
                    direction_ = '↑'
                }
            }
            else if (Ydistance <= 0 && Ydistance > -2)
            {
                if(absXdistance < absYdistance)
                {
                    direction_ = '↑'
                }
                else if (absXdistance == absYdistance)
                {
                    direction_ = '↑'
                }
                else if (absXdistance > absYdistance)
                {
                    direction_ = '↑'
                }
            }
            else if (Ydistance <= -2 )
            {
                if(absXdistance < absYdistance)
                {
                    direction_ = '↓'
                }
                else if (absXdistance == absYdistance)
                {
                    direction_ = '↓'
                }
                else if (absXdistance > absYdistance)
                {
                    direction_ = '↓'
                }
            }
        }
        else if (Xdistance <= -2)
        {
            if (Ydistance > 0)
            {
                if(absXdistance < absYdistance)
                {
                    direction_ = '↑'
                }
                else if (absXdistance == absYdistance)
                {
                    direction_ = '↑'
                }
                else if (absXdistance > absYdistance)
                {
                    direction_ = '→'
                }
            }
            else if (Ydistance <= 0 && Ydistance > -2)
            {
                if(absXdistance < absYdistance)
                {
                    direction_ = '→'
                }
                else if (absXdistance == absYdistance)
                {
                    direction_ = '→'
                }
                else if (absXdistance > absYdistance)
                {
                    direction_ = '→'
                }
            }
            else if (Ydistance <= -2)
            {
                if(absXdistance < absYdistance)
                {
                    direction_ = '↓'
                }
                else if (absXdistance == absYdistance)
                {
                    direction_ = '→'
                }
                else if (absXdistance > absYdistance)
                {
                    direction_ = '→'
                }
            }
        }
        if (direction_ == null) return null
        return direction_
}

/* 进行转向 */
export function ChangeArray(squardData:Squard,Attackdirection:string,direction_:string):void{
        /* 根据自己的方向进行旋转 */
        if (Attackdirection == '←')
        {
            switch(direction_)
            {
                case '←':{break;}
                case '→':{squardCross(squardData);break}
                case '↓':{squardLeft(squardData);break}
                case '↑':{squardRight(squardData);break}
            }
        }
        else if (Attackdirection == '→')
        {
            switch(direction_)
            {
                case '←':{squardCross(squardData);break;}
                case '→':{break;}
                case '↓':{squardRight(squardData);break}
                case '↑':{squardLeft(squardData);break}
            }
        }
        else if(Attackdirection == '↑')
        {
            switch(direction_)
            {
                case '←':{squardLeft(squardData);break}
                case '→':{squardRight(squardData);break}
                case '↓':{squardCross(squardData);break}
                case '↑':{break}
            }
        }
        else if (Attackdirection == '↓')
        {
            switch(direction_)
            {
                case '←':{squardRight(squardData);break}
                case '→':{squardLeft(squardData);break}
                case '↓':{break}
                case '↑':{squardCross(squardData);break}
            }
        }
}

/* 根据指定位置调整攻击姿势 */
// export function changeArray(squardData:Squard,pos:RoomPosition):void{
//     var Attackdirection = squardAttackDirection(squardData)
//     if (Attackdirection == null) return
//     /* 计算位置和小队的距离 */
//     var standCreep = getStandCreep(squardData)
//     if (!standCreep) return
//     var Xdistance = standCreep.pos.x - pos.x
//     var direction_:string = null
//     var Ydistance = standCreep.pos.y - pos.y
//     var absXdistance = Math.abs(Xdistance)
//     var absYdistance = Math.abs(Ydistance)
//     if (Xdistance > 0)
//     {
//         if (Ydistance > 0)
//         {
//             if(absXdistance < absYdistance)
//             {
//                 direction_ = '↑'
//             }
//             else if (absXdistance == absYdistance)
//             {
//                 direction_ = '←'
//             }
//             else if (absXdistance > absYdistance)
//             {
//                 direction_ = '←'
//             }
//         }
//         else if (Ydistance <= 0 && Ydistance > -2)
//         {
//             if(absXdistance < absYdistance)
//             {
//                 direction_ = '←'
//             }
//             else if (absXdistance == absYdistance)
//             {
//                 direction_ = '←'
//             }
//             else if (absXdistance > absYdistance)
//             {
//                 direction_ = '←'
//             }
//         }
//         else if (Ydistance <= -2 )
//         {
//             if(absXdistance < (absYdistance- 1))
//             {
//                 direction_ = '↓'
//             }
//             else if (absXdistance == (absYdistance- 1))
//             {
//                 direction_ = '←'
//             }
//             else if (absXdistance > (absYdistance- 1))
//             {
//                 direction_ = '←'
//             }
//         }

//     }
//     else if (Xdistance <= 0 && Xdistance >-2)
//     {
//         if (Ydistance > 0)
//         {
//             if(absXdistance < absYdistance)
//             {
//                 direction_ = '↑'
//             }
//             else if (absXdistance == absYdistance)
//             {
//                 direction_ = '↑'
//             }
//             else if (absXdistance > absYdistance)
//             {
//                 direction_ = '↑'
//             }
//         }
//         else if (Ydistance <= 0 && Ydistance > -2)
//         {
//             if(absXdistance < absYdistance)
//             {
//                 direction_ = '↑'
//             }
//             else if (absXdistance == absYdistance)
//             {
//                 direction_ = '↑'
//             }
//             else if (absXdistance > absYdistance)
//             {
//                 direction_ = '↑'
//             }
//         }
//         else if (Ydistance <= -2 )
//         {
//             if(absXdistance < absYdistance)
//             {
//                 direction_ = '↓'
//             }
//             else if (absXdistance == absYdistance)
//             {
//                 direction_ = '↓'
//             }
//             else if (absXdistance > absYdistance)
//             {
//                 direction_ = '↓'
//             }
//         }
//     }
//     else if (Xdistance <= -2)
//     {
//         if (Ydistance > 0)
//         {
//             if(absXdistance < absYdistance)
//             {
//                 direction_ = '↑'
//             }
//             else if (absXdistance == absYdistance)
//             {
//                 direction_ = '↑'
//             }
//             else if (absXdistance > absYdistance)
//             {
//                 direction_ = '→'
//             }
//         }
//         else if (Ydistance <= 0 && Ydistance > -2)
//         {
//             if(absXdistance < absYdistance)
//             {
//                 direction_ = '→'
//             }
//             else if (absXdistance == absYdistance)
//             {
//                 direction_ = '→'
//             }
//             else if (absXdistance > absYdistance)
//             {
//                 direction_ = '→'
//             }
//         }
//         else if (Ydistance <= -2)
//         {
//             if(absXdistance < absYdistance)
//             {
//                 direction_ = '↓'
//             }
//             else if (absXdistance == absYdistance)
//             {
//                 direction_ = '→'
//             }
//             else if (absXdistance > absYdistance)
//             {
//                 direction_ = '→'
//             }
//         }
//     }
//     if (direction_ == null) return

//     /* 根据自己的方向进行旋转 */
//     if (Attackdirection == '←')
//     {
//         switch(direction_)
//         {
//             case '←':{break;}
//             case '→':{squardCross(squardData);break}
//             case '↓':{squardLeft(squardData);break}
//             case '↑':{squardRight(squardData);break}
//         }
//     }
//     else if (Attackdirection == '→')
//     {
//         switch(direction_)
//         {
//             case '←':{squardCross(squardData);break;}
//             case '→':{break;}
//             case '↓':{squardRight(squardData);break}
//             case '↑':{squardLeft(squardData);break}
//         }
//     }
//     else if(Attackdirection == '↑')
//     {
//         switch(direction_)
//         {
//             case '←':{squardLeft(squardData);break}
//             case '→':{squardRight(squardData);break}
//             case '↓':{squardCross(squardData);break}
//             case '↑':{break}
//         }
//     }
//     else if (Attackdirection == '↓')
//     {
//         switch(direction_)
//         {
//             case '←':{squardRight(squardData);break}
//             case '→':{squardLeft(squardData);break}
//             case '↓':{break}
//             case '↑':{squardCross(squardData);break}
//         }
//     }
// }


